package com.qiantu.phone.ui.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hjq.http.listener.HttpCallback;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.XXPermissions;
import com.hjq.toast.ToastUtils;
import com.hjq.widget.layout.SettingBar;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.aop.Log;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.scan.ScanCodeActivity;

import java.util.List;
import java.util.Map;

import okhttp3.Call;

public final class ControllerReplaceActivity extends AppActivity {
    Boolean isSlave;
    private SettingBar nameSettingBar;
    private SettingBar idSettingBar;
    private EditText controllerIDText;
    private TextView tvTips;
    private ImageView imgStep2;
    private LinearLayout layoutStep2;
    private TextView tvConfigWifi;
    private TextView tvOldController;
    private TextView tvNewController;
    private TextView tvNewControllerID;

    @Log
    public static void start(Context context, String serialNo, String controllerID, String name,Boolean isSlave,String productModel) {
        Intent intent = new Intent(context, ControllerReplaceActivity.class);
        intent.putExtra("serialNo", serialNo);
        intent.putExtra("controllerID", controllerID);
        intent.putExtra("name", name);
        intent.putExtra("isSlave",isSlave);
        intent.putExtra("productModel",productModel);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_controller_replace;
    }

    @Override
    protected void initView() {
        imgStep2 = findViewById(R.id.step_2_check);
        layoutStep2 = findViewById(R.id.step_2_text);
        nameSettingBar = findViewById(R.id.tv_controller_name);
        idSettingBar = findViewById(R.id.tv_controller_id);
        controllerIDText = findViewById(R.id.edit_new_controllerID);
        tvConfigWifi = findViewById(R.id.tv_config_wifi);
        tvTips = findViewById(R.id.tv_tips);
        tvOldController = findViewById(R.id.tv_old_controller);
        tvNewController = findViewById(R.id.tv_new_controller);
        tvNewControllerID = findViewById(R.id.tv_new_controller_id);
        nameSettingBar.setRightText(getIntent().getStringExtra("name"));
        idSettingBar.setRightText(getIntent().getStringExtra("controllerID"));
        setOnClickListener(R.id.btn_scan,R.id.btn_replace);
        isSlave = getIntent().getBooleanExtra("isSlave",false);
        String productModel = getIntent().getStringExtra("productModel");
        if(!isSlave){
            tvTips.setText("1:确保新的主机处于联网状态且未被添加");
        }else if(productModel.equals("0x1503")){
            tvTips.setText("1.拆下旧设备并换上新设备\n2.在旧设备替换页面确认名称及ID，并扫码输入新设备的ID\n3.点击替换按键，弹窗提示成功即可");
        }else{
            tvTips.setText("1.拆下旧设备并换上新设备\n2.给新设备分配地址使其上线，在APP中找到该新设备并删除\n3.在旧设备替换页面确认名称及ID，并扫码输入新设备的ID\n4.点击替换按键，弹窗提示成功即可");
        }
        imgStep2.setVisibility(isSlave ? View.GONE : View.VISIBLE);
        layoutStep2.setVisibility(isSlave ? View.GONE : View.VISIBLE);
        tvConfigWifi.setText(isSlave ? "替换" : "配置网络");
        tvOldController.setText(isSlave ? "旧的从机" : "旧的主机");
        nameSettingBar.setLeftText(isSlave ? "当前从机名称" : "当前主机名称");
        idSettingBar.setLeftText(isSlave ? "当前从机ID" : "当前主机ID");
        tvNewController.setText(isSlave ? "新的从机" : "新的主机");
        tvNewControllerID.setText(isSlave ? "新的从机ID" : "新的主机ID");
        controllerIDText.setHint(isSlave ? "请输入新的从机ID" : "请输入新的主机ID");
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_scan:
                gotoScanView();
                break;
            case R.id.btn_replace:
                String  newControllerID = controllerIDText.getText().toString();
                if(newControllerID == null || newControllerID.equals("")){
                    toast(isSlave ? "请输入新从机的ID后再进行操作" : "请输入新主机的ID后再进行操作");
                    break;
                }
                initReplace();
                break;
        }
    }

    private void gotoScanView(){
        XXPermissions xxPermissions = XXPermissions.with(getActivity());
        xxPermissions.permission(Manifest.permission.CAMERA)
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {
                            Intent intent = new Intent(getContext(), ScanCodeActivity.class);
                            intent.putExtra("scanType",1);
                            startActivityForResult(intent, (resultCode, data) -> {
                               if(resultCode == RESULT_OK){
                                    String deviceSn = data.getStringExtra("deviceSn");
                                    controllerIDText.setText(deviceSn);
                               }
                            });
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        ToastUtils.show(R.string.common_permission_fail_1);
                    }
                });
    }

    private void initReplace(){
        LLHttpManager.controllerReplaceInit(this, isSlave ? 2 : 1, getIntent().getStringExtra("serialNo"), controllerIDText.getText().toString(), new HttpCallback<HttpData<Map<String,Object>>>(this){
            @Override
            public void onStart(Call call) {
                showDialog();
            }

            @Override
            public void onSucceed(HttpData<Map<String,Object>> result) {
                super.onSucceed(result);
                gotoProgressView();
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }

            @Override
            public void onEnd(Call call) {
                hideDialog();
            }
        });
    }

    private void gotoProgressView(){
        ControllerReplaceSyncActivity.start(this, getIntent().getStringExtra("serialNo"), isSlave ? 2 : 1,isSlave);
    }
}
